﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Playkanaler.WebPageSource
{
    public class PageLink
    {

        public string name;
        public string description;
        public string url;
        public string imageurl;
        public bool selected = false;

        public Dictionary<string, object> data=new Dictionary<string,object>();

        public PageLink()
        {
        }
        public PageLink(string Name)
        {
            name = Name;
        }
        public PageLink(string Name, string Url)
        {
            name = Name;
            url = Url;
        }
    }
    public class BasicPage
    {
        private List<PageLink> items = new List<PageLink>();

        public PageLink Item;
        public bool HasVideoPlayer { get; set; }
        public string VideoUrl { get; set; }
        public string VideoTitle { get; set; }
        public bool PageLoaded { get; set; }

        public string Url { get; set; }

        public List<PageLink> Tabs { get; set; }

        int selectedTabIndex = -1;
        virtual public int SelectedTabIndex
        {
            get
            {
                if (selectedTabIndex == -1)
                {
                    for (int i = 0; i < Tabs.Count; i++)
                    {
                        if (Tabs[i].selected)
                        {
                            selectedTabIndex = i;
                            break;
                        }
                    }

                }
                return selectedTabIndex;
            }
            set
            {
                selectedTabIndex = value;
            }
        }


        public string ContentPageNavTemplate { get; set; }
        public string ContentPageNavTemplateReplaceStr { get; set; }
        public int NumContentPagesLoaded { get; set; }
        public int NumContentPages { get; set; }



        public List<PageLink> Items
        {
            get
            {               
                return items;
            }
            set
            {
                items = value;
            }
        }
        public BasicPage()
        {
            PageLoaded = false;
            NumContentPagesLoaded = 0;
            Tabs = new List<PageLink>();
        }
        public bool HasMorePages
        {
            get
            {
                return (NumContentPagesLoaded < NumContentPages);

            }
        }

        public string FixUrl(string ny)
        {
            if (ny.StartsWith("?"))
            {
                Uri uritemp = new Uri(Url);
                ny = "http://" + uritemp.Host + uritemp.LocalPath + ny ;                
            }
            else if(ny.StartsWith("/"))
            {
                Uri uritemp = new Uri(Url);
                ny = "http://" + uritemp.Host + ny;   
            }
            else if (!ny.StartsWith("http://"))
            {
                Uri uritemp = new Uri(Url);
                ny = "http://" + uritemp.Host + "/" + ny;   
            }


            return ny;
        }

        public void Load()
        {
            PageLoaded = true;
            LoadPage();
            
        }
        protected virtual bool LoadPage()
        {
            return false;

        }

        public virtual BasicPage LoadMoreItemPage()
        {

          

            if (!HasMorePages) throw new Exception("No more pages");
            BasicPage page = (BasicPage)Activator.CreateInstance(this.GetType());
            NumContentPagesLoaded++;
            page.Url = this.ContentPageNavTemplate.Replace(this.ContentPageNavTemplateReplaceStr, NumContentPagesLoaded.ToString());
            page.Load();
           
            return page;
        }



    }
}
